/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.ddtroopsconsole;

import com.beust.jcommander.JCommander;
import cz.muni.fi.pa165.ddtroops.dto.RaceDTO;
import cz.muni.fi.pa165.ddtroops.dto.SkillDTO;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.RaceService;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.SkillService;
import cz.muni.fi.pa165.ddtroopsconsole.service.RaceServiceImpl;
import cz.muni.fi.pa165.ddtroopsconsole.service.SkillServiceImpl;

/**
 *
 * @author newohybat
 */
public class Main {
    public static void main(String args[])
    {        
        Options options = new Options();
        JCommander jc = new JCommander(options);
        
        CreateRaceCommand createRace = new CreateRaceCommand();
        jc.addCommand("createrace", createRace);
        UpdateRaceCommand updateRace = new UpdateRaceCommand();
        jc.addCommand("updaterace", updateRace);
        
        CreateSkillCommand createSkill = new CreateSkillCommand();
        jc.addCommand("createskill", createSkill);
        UpdateSkillCommand updateSkill = new UpdateSkillCommand();
        jc.addCommand("updateskill", updateSkill);
        
        FindCommand findCommand = new FindCommand();
        jc.addCommand("find", findCommand);
        DeleteCommand deleteCommand = new DeleteCommand();
        jc.addCommand("delete", deleteCommand);
        
        if(args.length < 1) 
        {
            jc.usage();
            System.exit(1);
        }
        
        try
        {
            jc.parse(args);
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
            System.exit(1);
        }
        
        if(options.isHelp())
        {
            jc.usage();
            System.exit(0);
        }
        
        try
        {
            String url = "http://" + options.getServer() + ":" + options.getPort() + "/pa165/api/";
            SkillService ss = new SkillServiceImpl(url+"skill/");
            RaceService rs = new RaceServiceImpl(url+"race/");            
            switch(jc.getParsedCommand())
            {
                case "createrace":
                    rs.create(
                            rs.createNewRace(
                                createRace.getName(),
                                createRace.getDescription(), 
                                createRace.getStrength(), 
                                createRace.getDexterity(), 
                                createRace.getConstitution(), 
                                createRace.getIntelligence(), 
                                createRace.getWisdom(), 
                                createRace.getCharisma()));
                    break;
                case "updaterace":
                    RaceDTO r = rs.createNewRace(
                                updateRace.getName(),
                                updateRace.getDescription(), 
                                updateRace.getStrength(), 
                                updateRace.getDexterity(), 
                                updateRace.getConstitution(), 
                                updateRace.getIntelligence(), 
                                updateRace.getWisdom(), 
                                updateRace.getCharisma());
                    r.id = updateRace.getId();
                    rs.update(r);
                    break;
                case "createskill":
                    ss.create(
                            ss.createNewSkill(
                                createSkill.getName(), 
                                createSkill.getDescription(), 
                                createSkill.getProfession(), 
                                createSkill.getMinXP()));
                    break;
                case "updateskill":
                    SkillDTO s = ss.createNewSkill(
                                updateSkill.getName(), 
                                updateSkill.getDescription(), 
                                updateSkill.getProfession(), 
                                updateSkill.getMinXP());
                    s.id = updateSkill.getId();
                    ss.update(s);
                    break;
                case "find":
                    if(findCommand.getType().equals("skill"))
                        System.out.println("Returned object: " + ss.getById(findCommand.getId()));
                    else
                        System.out.println("Returned object: " + rs.getById(findCommand.getId()));
                    break;
                case "delete":
                    if(deleteCommand.getType().equals("skill"))
                    {
                        SkillDTO s1 = new SkillDTO();
                        s1.id = deleteCommand.getId();
                        ss.delete(s1);
                    }
                    else
                    {
                        RaceDTO r1 = new RaceDTO();
                        r1.id = deleteCommand.getId();
                        rs.delete(r1);
                    }
                    break;
            }
        }
        catch(Exception ex)
        {
            System.out.println("Error while connecting to dd-troops rest service.");
            System.exit(1);
        }
        
        System.exit(0);
    }
}
